package com.yzh.wanandroid.repository.datasource

import androidx.paging.PagingSource
import com.yzh.wanandroid.network.retrofit.RetrofitFactory
import com.yzh.wanandroid.network.retrofit.api.UserServer
import com.yzh.wanandroid.network.retrofit.responsebean.BaseData
import com.yzh.wanandroid.network.retrofit.responsebean.BasePageData
import com.yzh.wanandroid.network.retrofit.responsebean.IntegralData

class IntegralListRepository : PagingSource<Int, IntegralData>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, IntegralData> {
        return try {
            var currentPage = params.key ?: 0
            var integralList: BaseData<BasePageData<IntegralData>> =
                RetrofitFactory.createApi(UserServer::class.java).getIntegralList(currentPage)
            var nextPage = if (currentPage < integralList.data.curPage) {
                currentPage + 1
            } else {
                null
            }
            LoadResult.Page(
                data = integralList.data.datas,
                prevKey = null,
                nextKey = nextPage
            )
        } catch (e: Exception) {
            LoadResult.Error(throwable = e)
        }
    }
}